;Copyright 2007-2010 Niko Carpenter and Tyler Spivey
;    This file is part of Linna Home Server.
;    Linna Home is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    any later version.

;    Linna Home is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with Linna Home.  If not, see http://www.gnu.org/licenses/.

#include-once
const $iac=chr(255)
const $will=chr(251)
const $wont=chr(252)
const $echo_off = $iac&$will&chr(1)
const $echo_on = $iac&$wont&chr(1)&$iac&chr(253)&chr(1)

Global $Helps[1][2]
Global $arg
Func ParseCMD($id,$lrecv)
While 1
if StringLeft($lrecv,1) = chr(255) Then
$lrecv = StringTrimLeft($lrecv,3)
Else
ExitLoop
EndIf
Wend
$lrecv = BackSpace($LRecv)
FileChangeDir($Cons[$id][$C_Working])
Call("P_"&$Cons[$id][$C_Parser],$ID,$lrecv)
$Cons[$id][$C_Working] = @Workingdir
if @Error Then $Cons[$id][$C_Parser] = "lin"
EndFunc

func lreturn($id)
$cons[$id][$C_Parser] = "lin"
$cons[$id][$C_pdata] = ""
$cons[$id][$C_pdata2] = ""
$cons[$id][$C_pdata3] = ""
$cons[$id][$C_pdata4] = ""
$cons[$id][$C_pdata5] = ""
$cons[$id][$C_pdata6] = ""
$cons[$id][$C_pdata7] = ""
Prompt($id)
EndFunc
Func addcmd($cmd,$desc)
local $dems = ubound($Helps)
redim $Helps[$Dems+1][2]
$Helps[$Dems][0] = $CMD
$Helps[$Dems][1] = $Desc
EndFunc
Func Prompt($id)
local $t = "$"
if $Cons[$id][$C_Trust] = 1 Then $t = "#"
$Prompt = StringReplace($cons[$id][$C_Prompt],"%u",$Cons[$id][$C_User])
$Prompt = StringReplace($Prompt,"%h",@Computername)
$Prompt = StringReplace($Prompt,"%w",StringReplace(@WorkingDir,@ScriptDir,"~"))
$Prompt = StringReplace($Prompt,"%t",$t)
$Prompt = StringReplace($Prompt,@lf,"%")
TCPSend(SRVLib_GetSock($id),$Prompt)
EndFunc

Func P_Lin($Id,$Arg)
if $arg = @lf Then
Prompt($id)
return
EndIf
$args = StringRegExp($arg,"^\W*(\w+)\s*(.*)$",1)
if @Error Then
return
EndIf
If ChkPrm($id,$args[0]) Then
$ret = Call("lin_"&$args[0],$id,$args[1])
if @Error Then
	dsend($id,"Linna: "&$args[0]&": Command not supported")
Else
$msg = ""
if $logallcmd = 1 Then
$msg = $args[0]
ElseIf $args[0] = "passwd" and $logallcmd = 2 Then
$msg = $args[0]
ElseIf $logallcmd = 2 Then
$msg = $arg
EndIf
if $msg <> "" Then LogWrite("command from "&$cons[$id][$C_User]&" ("&$id&"): "&$msg)
	EndIf
Else
dSend($Id,"Linna: "&$args[0]&": permission denied.")
EndIf
if SRVLib_GetSock($id) >= 0 Then
if $cons[$id][$C_Parser] = "lin" Then Prompt($id)
EndIf
EndFunc
Func p_auth_user($id,$arg)
$Cons[$id][$C_User] = $arg
$cons[$id][$C_Parser] = "auth_password"
TCPSend(SRVLib_GetSock($id),"Password: "&$echo_off)
EndFunc
Func p_auth_password($id,$arg)
$ps = iniread(@ScriptDir&"\Users.ini",$Cons[$id][$C_User],"Pass",Random(1,10000,0))
if $arg = _dkey($ps,"hmlin"&$arg&$cons[$id][$C_user]&$arg,0,1) Then
$Cons[$id][$C_Trust] = iniread(@ScriptDir&"\Users.ini",$Cons[$id][$C_User],"Trust","0")
dSend($id,@LF&"Logged in."&$echo_on)
LogWrite("Authorised "&SRVLib_SocketToIp(SRVLib_GetSock($id))&" as "&$Cons[$id][$C_User]&"  ("&$id&").")
lreturn($id)
Else
LogWrite("Denied: "&SRVLib_SocketToIp(SRVLib_GetSock($id))&".")
dSend($id,@LF&"Invalid username/password. Goodbye."&$echo_on)
SRVLib_CloseConnection("ConClose",$Id)
EndIf
EndFunc
